package com.hanbang.chu_xing_gao_zhao.view_dynamic;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic;
import com.hanbang.chu_xing_gao_zhao.utils.ScreenUtils;
import com.hanbang.cxgz_2.R;

public class MorePopupWindowDynamic extends PopupWindow implements OnClickListener {
	public View conentView;
	
	private int count;
	private Context context;
	

	// 对话框点击事件
	private OnClickCallback onClickCallback = null;

	public MorePopupWindowDynamic(final Context context, int viewId) {
		
		this.context=context;

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		/** 得到视图 */
		conentView = inflater.inflate(viewId, null);

//		int h = context.getWindowManager().getDefaultDisplay().getHeight();
//		int w = context.getWindowManager().getDefaultDisplay().getWidth();

		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);

		// 设置SelectPicPopupWindow弹出窗体的宽
		// this.setWidth(w / 2 + 50);

		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);

		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);

		// 刷新状态
		this.update();

		// 实例化一个ColorDrawable背景颜色
		ColorDrawable dw = new ColorDrawable(0xffffffff);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);

		setOnClickListener();

	}

	/**
	 * 对话框的显示位置
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {

		if (!this.isShowing()) {
			// 设置控件宽度，是对对象控件 的2倍
//			this.setWidth(ScreenUtils.getScreenWidth(context)/3);
			this.setWidth((int)(parent.getWidth()/1.5));
			// 显示位置在对象控件的下面，宽度的1/2的前面
			this.showAsDropDown(parent, parent.getWidth()/6, 5);
			
//			this.showAtLocation(parent, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
			
		} else {

			this.dismiss();
		}

	}

	/**
	 * 添加监听
	 */
	private void setOnClickListener() {
		ArrayList<View> parentView = new ArrayList<View>();

		// 先把所有的控件放入集合
		parentView.add(conentView.findViewById(R.id.item1));
		parentView.add(conentView.findViewById(R.id.item2));
		parentView.add(conentView.findViewById(R.id.item3));
		parentView.add(conentView.findViewById(R.id.item4));
		parentView.add(conentView.findViewById(R.id.item5));
		parentView.add(conentView.findViewById(R.id.item6));
		parentView.add(conentView.findViewById(R.id.item7));
		parentView.add(conentView.findViewById(R.id.item8));
		parentView.add(conentView.findViewById(R.id.item9));
		parentView.add(conentView.findViewById(R.id.item10));
		parentView.add(conentView.findViewById(R.id.item11));
		// 遍历添加监听
		for (int i = 0; i < parentView.size(); i++) {
			parentView.get(i).setOnClickListener(this);
		}

	}

	/**
	 * 监听的处理
	 */
	@Override
	public void onClick(View v) {

		if (onClickCallback != null) {
			/**
			 * 实现接口并赋值
			 */
			onClickCallback.onClick(MorePopupWindowDynamic.this, v.getId());
			dismiss();

		} else {
			dismiss();
		}

	}

	/**
	 * 接口
	 */
	public interface OnClickCallback {

		public void onClick(MorePopupWindowDynamic morePopWindow, Object number);

	}

	/**
	 * 当设置监听时必须实现接口
	 * 
	 * @param callback
	 */
	public void setOnClickCallback(OnClickCallback callback) {
		onClickCallback = callback;
	}

}
